package cn.ecut.servlet;

import cn.ecut.model.News;
import cn.ecut.services.Newsservices;
import cn.ecut.util.StatusCode;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet("news/list")
public class FindNewsServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String issort=req.getParameter("issort");
        String findtext = req.getParameter("findtext");
        Newsservices newsservices = new Newsservices();
        List<News> newsList = new ArrayList<>();
        if (issort.equals("1")) {
            newsList = newsservices.FindNewsBySort(findtext);
        } else {
            newsList = newsservices.FindNewsByTitle(findtext);
        }
        Map<String, Object> map = new HashMap<>();
        if (newsList.size() > 0) {
            map.put("data", newsList);
            map.put("status", StatusCode.SUCCESS);
            map.put("msg", "成功");
        } else {
            map.put("status", StatusCode.SERVER_ERROR);
            map.put("msg", "失败");
        }
        resp.getWriter().write(JSON.toJSONString(map));
    }
}
